//
//  PickerAlertView.swift
//  MIFI
//
//  Created by 金嗓子卡卡 on 2019/5/3.
//  Copyright © 2019 NJMifi. All rights reserved.
//

import UIKit

class PickerAlertView: UIView ,
AlertAnimationViewProtocol,
UIPickerViewDelegate,
UIPickerViewDataSource{

    var handle : ((Int)->())?
    var index = 0 {
        didSet {
//            pickerView.selectedRow(inComponent: index)
        }
    }
    private var selectedIndex = 0
    private var data : [String] = []
    private var title = ""
    private lazy var cancelBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.frame = CGRect(x: 0, y: 0, width: 80, height: 40)
        btn.setTitle("Cancel", for: .normal)
        btn.setTitleColor(UIColor.blue, for: .normal)
        btn.backgroundColor = clearColor
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
        btn.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
        return btn
    }()
    
    private lazy var finishBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.frame = CGRect(x: 0, y: 0, width: 60, height: 40)
        btn.right = screenWidth
        btn.setTitle("OK", for: .normal)
        btn.setTitleColor(UIColor.blue, for: .normal)
        btn.backgroundColor = clearColor
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
        btn.addTarget(self, action: #selector(finishAction), for: .touchUpInside)
        return btn
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: cancelBtn.right, y: 0, width: finishBtn.left - cancelBtn.right, height: 40))
        label.text = title
        label.textAlignment = .center
        label.textColor = blackColor
        label.font = UIFont.boldSystemFont(ofSize: 18)
        label.backgroundColor = clearColor
        return label
    }()
    
    private lazy var pickerView: UIPickerView = {
        let pick = UIPickerView(frame: CGRect(x: 0, y: titleLabel.bottom, width: width, height: height - titleLabel.bottom))
        pick.delegate = self
        pick.dataSource = self
        pick.backgroundColor = whiteColor
        pick.selectedRow(inComponent: 0)
        return pick
    }()
    
    init(title:String,data:[String]) {
        super.init(frame: .zero)
        self.data = data
        self.title = title
        configView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configView() {
        frame = CGRect(x: 0, y: screenHeight, width: screenWidth, height: 340)
        backgroundColor = colorRGBA(red: 230, green: 230, blue: 230, alpha: 1)
        addSubview(cancelBtn)
        addSubview(finishBtn)
        addSubview(titleLabel)
        addSubview(pickerView)
    }
    
    @objc private func cancelAction() {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "CloseCustomAlertVC"), object: nil)
    }
    
    @objc private func finishAction() {
        if handle != nil {
            handle!(selectedIndex)
        }
        cancelAction()
    }
    
    func showAnimation() {
        UIView.animate(withDuration: 0.2) {[weak self] in
            self?.bottom = screenHeight
        }
    }
    
    func dismissAnimation(finish: @escaping (() -> ())) {
        UIView.animate(withDuration: 0.2, animations: {[weak self] in
            self?.top = screenHeight
        }) { (finished) in
            if finished {
                finish()
            }
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return data.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return data[row]
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        selectedIndex = row
    }
    
}
